package com.code.family.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.code.family.entity.enums.CategoryType;
import com.code.family.entity.pojo.Transactions;
import com.code.family.entity.vo.RankVo;
import com.code.family.entity.vo.TotalVo;
import com.code.family.service.TransactionsService;
import com.code.family.utils.ResultVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 首页数据统计
 * @author 上玄
 * @since 2024-12-24
 */
@RestController
@RequestMapping("/api/count")
@Slf4j
@RequiredArgsConstructor
public class HomeController {

    private final TransactionsService transactionsService;

    /**
     * 统计本月家庭收入和支出
     */
    @GetMapping("/getTotal")
    public ResultVo incomeExpense() {
        LambdaQueryWrapper<Transactions> queryWrapper = new LambdaQueryWrapper<>();
        // 获取当前日期
        LocalDate now = LocalDate.now(ZoneId.systemDefault());
        // 获取当前月份
        YearMonth currentYearMonth = YearMonth.from(now);
        // 获取本月的第一天
        LocalDate firstDayOfMonth = currentYearMonth.atDay(1);
        // 获取本月的最后一天
        LocalDate lastDayOfMonth = currentYearMonth.atEndOfMonth();
        log.info("当前{}",firstDayOfMonth);
        // 构建查询
        queryWrapper.ge(Transactions::getDate,firstDayOfMonth)
                .le(Transactions::getDate,lastDayOfMonth);
        // 查询
        List<Transactions> transactions = transactionsService.list(queryWrapper);
        log.info("列表{}",transactions);
        // 统计收入条数
        int countType0 = (int) transactions.stream()
                .filter(t -> t.getType() == 0)
                .count();
        double totalAmountType0 = transactions.stream()
                .filter(t -> t.getType() == 0)
                .mapToDouble(Transactions::getAmount)
                .sum();
        log.info("type为0的条数: {},总金额: {}", countType0,totalAmountType0);
        // 统计支出条数
        int countType1 = (int) transactions.stream()
                .filter(t -> t.getType() == 1)
                .count();
        double totalAmountType1 = transactions.stream()
                .filter(t -> t.getType() == 1)
                .mapToDouble(Transactions::getAmount)
                .sum();
        log.info("type为1的条数：{},总金额：{}",countType1,totalAmountType1);
        // 构建返回结果
        TotalVo vo = new TotalVo();
        vo.setTotalIncome(totalAmountType0);
        vo.setTotalExpense(totalAmountType1);
        vo.setCountIncome(countType0);
        vo.setCountExpense(countType1);
        return ResultVo.success(vo);
    }


    /**
     * 统计收入和支出排行榜
     */
    @GetMapping("/getRank")
    public ResultVo rank() {
        // 查询本月的记录
        LambdaQueryWrapper<Transactions> queryWrapper = new LambdaQueryWrapper<>();
        // 获取当前日期
        LocalDate now = LocalDate.now(ZoneId.systemDefault());
        // 获取当前月份
        YearMonth currentYearMonth = YearMonth.from(now);
        // 获取本月的第一天
        LocalDate firstDayOfMonth = currentYearMonth.atDay(1);
        // 获取本月的最后一天
        LocalDate lastDayOfMonth = currentYearMonth.atEndOfMonth();
        log.info("当前{}",firstDayOfMonth);
        // 构建查询
        queryWrapper.ge(Transactions::getDate,firstDayOfMonth)
                .le(Transactions::getDate,lastDayOfMonth);
        // 查询
        List<Transactions> transactions = transactionsService.list(queryWrapper);
        log.info("列表{}",transactions);
        // 统计每个分类的收入和支出
        // 收入
        Map<CategoryType,Double> incomeMap = transactions.stream()
                .filter(t -> t.getType() == 0)
                .collect(Collectors.groupingBy(Transactions::getCategory,Collectors.summingDouble(Transactions::getAmount)));
        log.info("收入{}",incomeMap);
        // 支出
        Map<CategoryType,Double> expenseMap = transactions.stream()
                .filter(t -> t.getType() == 1)
                .collect(Collectors.groupingBy(Transactions::getCategory,Collectors.summingDouble(Transactions::getAmount)));
        log.info("支出{}",expenseMap);
        // 构建返回结果
        RankVo vo = new RankVo();
        vo.setCategoryIncome(incomeMap);
        vo.setCategoryExpense(expenseMap);
        return ResultVo.success(vo);
    }
}
